package com.example.board.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.example.board.common.R;
import com.example.board.pojo.vo.*;
import com.example.board.service.SalesmanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api")
@Api(tags = "业务员数据看板",value = "业务员数据看板")
@RequiredArgsConstructor
public class SalesmanController {

    private final SalesmanService salesmanService;
    //全国经销商总额
    @PostMapping("/salesman-board/dept-daily")
    @ApiOperation(value = "部门管理日报表")
    public R getDeptDaily(String province, String region, String teamName, String startTime){
        BigDatasource bigDatasource = salesmanService.getDeptDaily(province,region,teamName,startTime);
        MiddleDatasource middleDatasource = salesmanService.getDeptMoth(province,region,teamName,startTime);
        Map<String,Object> map = new HashMap<>();
        map.put("bigDatasource",bigDatasource);
        map.put("middleDatasource",middleDatasource);
        return R.ok(map);
    }

    //全国经销商总额
    @PostMapping("/salesman-board/month-report")
    @ApiOperation(value = "业务员月报表")
    public R getMonthReport(String province, String region, String teamName,String startTime){
        //月订单统计数据
        MonthOrder monthOrder = salesmanService.getMonthOrder(province,region,teamName,startTime);

        //回访
        MonthOrder monthVitit =salesmanService.getMonthVitit(province,region,teamName,startTime);

        //拜访
        MonthOrder monthReVitit =salesmanService.getMonthReVitit(province,region,teamName,startTime);

        Map<String,Object> map = new HashMap<>();
        map.put("monthOrder",monthOrder);
        map.put("visitCustom",monthVitit);
        map.put("revisitCustom",monthReVitit);
        return R.ok(map);
    }

    @PostMapping("/salesman-board/daily-report")
    @ApiOperation(value = "业务员管理日报表")
    public R getDailyReport(String province,String region,String teamName,String startTime){
        List<DailyChartData> data = salesmanService.getDailyReport(province,region,teamName,startTime);

        Map<String,Object> map = new HashMap<>();
        map.put("chartData",data);
        return R.ok(map);
    }

    @PostMapping("/salesman-board/salesman-analysis")
    @ApiOperation(value = "业务员业绩分析(逻辑修改)")
    public R getSalesmanAnalysis(String o_entry_time,
                                 String o_compare,
                                 String id
                                 ){

        //String province,
        // String region,
        // String teamName,
        System.out.println("oEntryTime: "+o_entry_time +" ::: "+o_compare);
//        System.out.println(province);

        Map<String, Object> orderAnalysis = salesmanService.getOrderAnalysis( o_entry_time, o_compare,id);


        return R.ok(orderAnalysis);

    }

    @GetMapping("/salesman-board/salesman-excel")
    @ApiOperation(value = "下载excel")
    public R getSalesmanExcel(String province,
                              String region,
                              String teamName,
                              HttpServletResponse response,
                              HttpServletRequest request) throws IOException {

        try {
            //获取数据
            List<ExcelSalesmanVo> data = salesmanService.findAll(province,region,teamName);
            if (CollectionUtils.isEmpty(data)) {
                data.add(new ExcelSalesmanVo());
            }
            //attachment指定独立文件下载  不指定则回浏览器中直接打开
            String fileName = "业务订单导出_" + DateUtil.formatDate(new Date()) + ".xlsx";
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            //导出excel
            EasyExcel.write(response.getOutputStream(), ExcelSalesmanVo.class).sheet("业务订单").doWrite(data);
            log.info("业务订单导出end");
        } catch (Exception e) {
            log.error("业务订单导出异常打印:", e);
        } finally {
            try {
                response.flushBuffer();
            } catch (IOException e) {
                log.error("业务订单导出输出流关闭失败: {}", e);
            }
        }

        return R.ok();
    }

}
